package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.PaymentMapper;
import cn.supermarket.pojo.dto.PaymentAddNewDTO;
import cn.supermarket.pojo.entity.Payment;
import cn.supermarket.pojo.vo.PaymentDetailsVO;
import cn.supermarket.pojo.vo.PaymentPlainVO;

import cn.supermarket.service.iservice.IPaymentService;
import cn.supermarket.utils.GetDateTime;
import cn.supermarket.utils.GetInfoUtils;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.State;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Service
public class PaymentServiceImpl implements IPaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    GetDateTime getDateTime;



    //添加收款单
    @Override
    public void addNewPayment(PaymentAddNewDTO paymentAddNewDTO) {
        Payment payment = new Payment();
        BeanUtils.copyProperties(paymentAddNewDTO, payment);
        SnowFlakeGenerateIdWorker sd = new SnowFlakeGenerateIdWorker(12, 1);
        Long id =sd.nextId();
        String or = sd.generateNextId();
        payment.setId(id);
        payment.setOrderNumber(or);
        payment.setIsEnable(0);
        payment.setIsDisplay(1);
        payment.setIsCancel(0);
        payment.setScanningCopy(LocalDateTime.now());
        payment.setLastUpdateTime(LocalDateTime.now());
        GetInfoUtils getInfoUtils = new GetInfoUtils();
        GetInfoUtils userInfo = getInfoUtils.getUserInfo();
        payment.setHandler(userInfo.getUsername());
        paymentMapper.addNewPayment(payment);
    }

    //修改可见
    @Override
    public int updateIsDisplay(Long id, Integer isDisplay) {
        PaymentPlainVO byId = paymentMapper.getId(id);
        if (byId == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "所查类别不存在");
        }
        if (byId.getIsDisplay().equals(isDisplay)) {
            throw new ServiceException(State.ERR_REPEAT_UPDATE, "该操作为无效的重复操作");
        }
        int rows;
        if (isDisplay == 1 || isDisplay == 0) {
             rows = paymentMapper.updateIsDisplay(id, isDisplay);
            paymentMapper.updateLastUpdateTime(id, LocalDateTime.now());
            GetInfoUtils getInfoUtils = new GetInfoUtils();
            GetInfoUtils userInfo = getInfoUtils.getUserInfo();
            paymentMapper.updateHandler(userInfo.getUsername(),id);
            if (rows != 1) {
                throw new ServiceException(State.ERR_UNKNOWN_ERROR, "未知错误导致修改失败，请稍后再试");
            }

        } else {
            throw new ServiceException(State.ERR_BAD_REQUEST, "参数错误");
        }
        return rows;
    }

    //锁定
    @Override
    public int updateIsCancel(Long id) {
        PaymentPlainVO byId = paymentMapper.getId(id);
        if (byId == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "所查类别不存在");
        }
        int rows = paymentMapper.updateIsCancel(id);
        GetInfoUtils getInfoUtils = new GetInfoUtils();
        GetInfoUtils userInfo = getInfoUtils.getUserInfo();
        paymentMapper.updateHandler(userInfo.getUsername(),id);
        if (rows != 1) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR, "未知错误导致修改失败，请稍后再试");
        }
        paymentMapper.updateLastUpdateTime(id, LocalDateTime.now());
        return rows;
    }


    //修改备注
    @Override
    public int updateOther(Long id, String other) {
        PaymentPlainVO byId = paymentMapper.getId(id);
        if (byId == null) {
            throw new ServiceException(State.ERR_NOT_FOUND, "所查类别不存在");
        }
        int rows = paymentMapper.updateOther(id, other);
        GetInfoUtils getInfoUtils = new GetInfoUtils();
        GetInfoUtils userInfo = getInfoUtils.getUserInfo();
        paymentMapper.updateHandler(userInfo.getUsername(),id);
        if (rows != 1) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR, "未知错误导致修改失败，请稍后再试");
        }
        paymentMapper.updateLastUpdateTime(id,LocalDateTime.now());
        return rows;
    }

    //单号查询
    @Override
    public PaymentDetailsVO getOrderNumber(String orderNumber) {
        PaymentDetailsVO orderNumber1 = paymentMapper.getOrderNumber(orderNumber);
        if (orderNumber1==null){
            throw new ServiceException(State.ERR_NOT_FOUND, "该收款单不存在");
        }
        return orderNumber1;
    }

    //日期查询
    @Override
    public List<PaymentDetailsVO> getData(Date dateStart, Date dateEnd) {
        List<PaymentDetailsVO> byData = paymentMapper.getData(getDateTime.getDateStart(dateStart), getDateTime.getDateEnd(dateEnd));
        if (byData==null){
            throw new ServiceException(State.ERR_NOT_FOUND, "该日期不存在收款单");
        }
        return byData;
    }

    @Override
    public List<PaymentDetailsVO> getPayee(String payee) {
        List<PaymentDetailsVO> payee1 = paymentMapper.getPayee(payee);
        if (payee1==null){
            throw new ServiceException(State.ERR_NOT_FOUND, "没有此收款方");
        }
        return payee1;
    }

    @Override
    public List<PaymentDetailsVO> getPayer(String payer) {
        List<PaymentDetailsVO> payer1 = paymentMapper.getPayer(payer);
        if (payer1==null){
            throw new ServiceException(State.ERR_NOT_FOUND, "没有此收款方");
        }
        return payer1;
    }
}


